package com.ymatou.iapi.godmch.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.ymatou.iapi.godmch.parameter.createDepotBean;
import com.ymatou.iapi.godmch.parameter.deleteDepotBean;
import com.ymatou.iapi.godmch.service.createDepotCall;
import com.ymatou.iapi.godmch.service.deleteDepotCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.godmch.mch_depot_info;
import com.ymttest.database.model.godmch.mch_depot_infoExample;
import com.ymttest.database.sqlwapper.godmch.mch_depot_infoWapper;
import com.ymttest.utils.AgentUsers;
/**
*删除仓库信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_deleteDepot{
	private static deleteDepotBean bean;
	private static deleteDepotCall call;
	
	createDepotBean cbean=new createDepotBean ();
	createDepotCall ccall=new createDepotCall();
    	
	mch_depot_infoWapper depotWapper;
	mch_depot_infoExample example;
	
	//渠道商
	private static int sellerId = AgentUsers.rootUserId;
	private static String sellerName = "NickautoTest";
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("删除仓库信息");
	}

	@Before
	public void caseUp() {
		bean=new deleteDepotBean();
		call=new deleteDepotCall();
		
		depotWapper=new mch_depot_infoWapper();
		example=new mch_depot_infoExample();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	private void check_verify() throws InterruptedException {
		
		Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
		example.clear();
		example.createCriteria().andMchIdEqualTo(bean.getMchId()).andDepotIdEqualTo(bean.getDepotId());
		mch_depot_info  depot=depotWapper.selectByExample(example).get(0);
       	Logger.verifyEquals(-1, depot.getDepotStatus(), "验证数据库字段DepotStatus=-1");
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_deleteDepot_001() {
		Logger.start(true,"删除仓库信息--验证商品未使用仓库");
		try {
			
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			GodmchManageCallService.createDepot(mchId);
			
			//查询DepotId
			example.clear();
			example.createCriteria().andMchIdEqualTo(mchId);
			mch_depot_info  info=depotWapper.selectByExample(example).get(0);
			
			bean.setMchId(mchId);
			bean.setDepotId(info.getDepotId());
			call.setData(bean);
			call.callService();
			
			check_verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_deleteDepot_002() {
		Logger.start(true,"删除仓库信息--验证商品已使用仓库");
		try {
			//ymt_products  iboundarea  这个是存的仓库id,表示商品使用了仓库
			GodmchManageCallService.createDepot(sellerId);
			//查询DepotId
			example.clear();
			example.createCriteria().andMchIdEqualTo(sellerId);
			example.setOrderByClause(" createTime desc ");
			mch_depot_info  info=depotWapper.selectByExample(example).get(0);
			
			//商品使用仓库
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,
                     sellerName, false);
           addAgentProductBean.setDepotId(info.getDepotId());
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
           Thread.sleep(5000);//创建完商品后需要同步到es，然后商户站点取es中取值,所以修改5秒等待同步es
			bean.setMchId(sellerId);
			bean.setDepotId(info.getDepotId());
			call.setData(bean);
			call.callService();
			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("抱歉，该仓库有关联中的商品，无法删除"), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_deleteDepot_003() {
		Logger.start(true,"删除仓库信息--验证一个商户两个仓库,其中一个仓库已使用商品,删除当前已使用的仓库");
		try {
			//ymt_products  iboundarea  这个是存的仓库id,表示商品使用了仓库
			GodmchManageCallService.createDepot(sellerId);
			GodmchManageCallService.createDepot(sellerId);
			//查询DepotId
			example.clear();
			example.createCriteria().andMchIdEqualTo(sellerId);
			example.setOrderByClause(" createTime desc ");
			mch_depot_info  info=depotWapper.selectByExample(example).get(0);
			
			//商品使用仓库
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,
                     sellerName, false);
            addAgentProductBean.setDepotId(info.getDepotId());
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
            Thread.sleep(5000);//创建完商品后需要同步到es，然后商户站点取es中取值,所以修改5秒等待同步es
			bean.setMchId(sellerId);
			bean.setDepotId(info.getDepotId());
			call.setData(bean);
			call.callService();
			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("抱歉，该仓库有关联中的商品，无法删除"), "验证msg");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_deleteDepot_004() {
		Logger.start(true,"删除仓库信息--验证一个商户两个仓库,其中一个仓库已使用商品,删除未使用的仓库");
		try {
			//ymt_products  iboundarea  这个是存的仓库id,表示商品使用了仓库
			GodmchManageCallService.createDepot(sellerId);
			GodmchManageCallService.createDepot(sellerId);
			//查询DepotId
			example.clear();
			example.createCriteria().andMchIdEqualTo(sellerId);
			example.setOrderByClause(" createTime desc ");
			mch_depot_info  info=depotWapper.selectByExample(example).get(0);//时间排序 第一条表示已使用，第二条表示未使用，
			
			//商品使用仓库
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,
                     sellerName, false);
           addAgentProductBean.setDepotId(info.getDepotId());
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
     
			bean.setMchId(sellerId);
			bean.setDepotId(depotWapper.selectByExample(example).get(1).getDepotId());
			call.setData(bean);
			call.callService();
			
			check_verify();	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}